#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
long f[201] = { 0 }, w[201] = { 0 }, c[201];
bool a[201][201] = { 0 };
long n;
long x, y;
void print()
{
	int k = 1;
	for (int m = 2; m <= n; ++m) {
		if (f[m] > f[k])
		{
			k = m;
		}
	}
	int ans = f[k];
	cout << k;
	k = c[k];
	while (k != 0) {
		cout << "-" << k;
		k = c[k];

	}
	
	cout << endl;
	cout << ans << endl;
}
void   inti()
{
	memset(a, false, sizeof(a));
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> w[i];
	}
	do {
		cin >> x >> y;
		if ((x != 0) && (y != 0)) {
			a[x][y] = true;

		}

	} while ((x != 0) || (y != 0));
	f[n] = w[n];
}
int main() {
	inti();

	for (int j = n - 1; j >= 1; j--) {
		int l = 0;
		for (int i = j + 1; i <= n; ++i) {
			if (a[j][i] && (l < f[i])) {
				l = f[i];
				c[j] = i;
			}
		}
		f[j] = l + w[j];
	}

	print();

	return 0;


}